alter PROCEDURE getComboReport @BatchNumber varchar(200),
    --工单状态
                                @BoStatus NVARCHAR(255),
                                @ProductionLineId NVARCHAR(50),
                                @khdm VARCHAR(50),
                                @begingmt_create DATETIME,
                                @endgmt_create DATETIME,
                                @PageIndex INT,
                                @PageSize INT,
                                @TotalCount INT OUTPUT
AS
BEGIN
    SET NOCOUNT ON;

    -- 计算分页的起始位置
    DECLARE @Offset INT = (@PageIndex - 1) * @PageSize;
--------------------------------------------------------------------------------------------
    SELECT b.production_line                                    AS 产线,

           coalesce(b.production_method, mo.production_methods) AS Production_Method,

           coalesce(b.customer_code, ms.customer_number)         AS 客户代码,

           coalesce(b.customer_name, ms.customer_name)           AS 客户名称,
           c.batch_created_date                                 AS batch_created_date,
           a.combo_name                                         AS combo名称,
           a.batch_number                                       AS 主子BO号,
           a.job                                                AS DJ,
           a.customer_po_number                                 AS po号,
           a.item_code                                          AS 产品名称,
           a.job_qty                                            AS 数量,
           d.name                                               AS 状态,
           a.order_number                                       AS order_number,
           -- 新增的combo序号字段
           DENSE_RANK() OVER (
               ORDER BY a.customer_po_number, a.job_qty, a.order_number
               )                                                AS combo序号,
           b.planner_code                                       AS planner_code,
           mes_order_create_time                                AS MES订单创建时间,
           ob.line_number                                       AS line_number


    FROM mes_bo_combo_result a
             LEFT JOIN
         mes_batch_order b ON a.batch_number = b.batch_number
             LEFT JOIN
         mes_or_batch c ON c.batch_number = a.batch_number
             LEFT JOIN
         sys_dict d ON d.value = b.status AND d.type = 'ORDER_STATUS'
             outer apply (select top 1 line_number
                          from oracle_batch ob
                          where ob.batch_number = a.batch_number) ob
             outer apply (select top 1 production_methods
                          from mes_or_batch mo
                          where mo.batch_number = a.batch_number) mo

             outer apply (select top 1 customer_number, customer_name
                          from mes_or_sol ms
                          where ms.batch_number = a.batch_number) ms



------------------------------------------------------------------------------------------------------


    WHERE (@BatchNumber IS NULL OR a.batch_number = @BatchNumber)
      AND (@begingmt_create IS NULL OR a.gmt_create >= @begingmt_create)
      AND (@endgmt_create IS NULL OR a.gmt_create <= @endgmt_create)
      AND (@BoStatus IS NULL OR b.status in (SELECT value FROM STRING_SPLIT(@BoStatus, ',')))
      AND (@ProductionLineId IS NULL OR b.production_line_id = @ProductionLineId)
      AND (@khdm IS NULL OR b.customer_code = @khdm)


    order by a.customer_po_number, a.job_qty, a.order_number
    OFFSET @Offset ROWS FETCH NEXT @PageSize ROWS ONLY;

    SELECT @TotalCount = COUNT(1)

    ---------------------------------------------------------------------

    FROM mes_bo_combo_result a
             LEFT JOIN
         mes_batch_order b ON a.batch_number = b.batch_number
             LEFT JOIN
         mes_or_batch c ON c.batch_number = a.batch_number
             LEFT JOIN
         sys_dict d ON d.value = b.status AND d.type = 'ORDER_STATUS'
             outer apply (select top 1 line_number
                          from oracle_batch ob
                          where ob.batch_number = a.batch_number) ob
             outer apply (select top 1 production_methods
                          from mes_or_batch mo
                          where mo.batch_number = a.batch_number) mo

             outer apply (select top 1 customer_number, customer_name
                          from mes_or_sol ms
                          where ms.batch_number = a.batch_number) ms
    -------------------------------------------

    WHERE (@BatchNumber IS NULL OR a.batch_number = @BatchNumber)
      AND (@begingmt_create IS NULL OR a.gmt_create >= @begingmt_create)
      AND (@endgmt_create IS NULL OR a.gmt_create <= @endgmt_create)
      AND (@BoStatus IS NULL OR b.status in (SELECT value FROM STRING_SPLIT(@BoStatus, ',')))
      AND (@ProductionLineId IS NULL OR b.production_line_id = @ProductionLineId)
      AND (@khdm IS NULL OR b.customer_code = @khdm)

END